﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace work3
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        string url = "";
        string[] lines;
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            url = text1.Text; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //先检测是否路径是否为空是否正正确，若不正确（查询为空），则弹出提示消息
            if (isurl())
            {
                //在路径正确的情况下，先按照分隔符查询每行代码，一共有多少行
                lines = File.ReadAllLines(url);
                int orgline = lines.Length;
                //储存原来的单词总数
                int orgword = Countword(lines);
                label2.Text = label2.Text+orgline.ToString();
                label3.Text = label3.Text + orgword.ToString();
                //统计每个单词在文件中的出现次数,输出控制台中
                Dictionary<string, int> wordOccurrences = CountWordOccurrences(lines);
                foreach (var pair in wordOccurrences)
                {
                    Console.WriteLine(pair.Key + ": " + pair.Value);
                }
            
        }
            else
            {
                return;
            }
        }

        private Dictionary<string, int> CountWordOccurrences(string[] lines)
        {
            Dictionary<string,int> wordOccurrences = new Dictionary<string, int>();
            foreach (string line in lines)
            {
                // 使用正则表达式匹配单词
                string[] words = Regex.Split(line, @"\W+");

                foreach (string word in words)
                {
                    if (!string.IsNullOrWhiteSpace(word))
                    {
                        if (wordOccurrences.ContainsKey(word))
                        {
                            wordOccurrences[word]++;
                        }
                        else
                        {
                            wordOccurrences[word] = 1;
                        }
                    }
                }
            }

            return wordOccurrences;
        
        }
        private static string[] RemoveEmptyLinesAndComments(string[] lines)
        {
            List<string> cleanedLines = new List<string>();
            foreach (string line in lines)
            {
                string cleanedLine = line.Trim();

                // 删除空行和以//开头的注释
                if (!string.IsNullOrEmpty(cleanedLine) && !cleanedLine.StartsWith("//"))
                {
                    cleanedLines.Add(cleanedLine);
                }
            }
            return cleanedLines.ToArray();
        }




        //利用正则表达式匹配单词
        private int Countword(string[] lines)
        {
            int count = 0;
            foreach(string line in lines)
            {
                string[] words = Regex.Split(line, @"W+");
                count += words.Length;
            }
            return count;
        }

        //检查路径是否正确和是否为空
        bool isurl()
        {
            if (url == "")
            {
                return false;
            }
            else if(isURLempty())
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        bool isURLempty()
        {
            if(!File.Exists(url))
            {
                //弹出消息框没有url
                MessageBox.Show("此路径没有找到任何文件", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }else
            {
                return true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //计数后删除空行和注释
            lines = RemoveEmptyLinesAndComments(lines);
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
